#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
User: lyj
DateTime: 2012-04-14 21:44:00

"""

__author__ = 'lyj'



from demos.rabbitmq.settings import amqp_host, amqp_user, amqp_passwd
from amqplib import client_0_8 as amqp


class Producer(object):

    def __init__(self, exchange_name, host, userid, password):
        """
        Constructor. Initiate connection with the RabbitMQ server.

        @param exchange_name name of the exchange to send messages to
        @param host RabbitMQ server host
        @param userid RabbitMQ server username
        @param password RabbitMQ server user's password
        """
        self.exchange_name = exchange_name
        self.connection = amqp.Connection(host=amqp_host, userid=amqp_user, password=amqp_passwd, virtual_host="/", insist=False)
        self.channel = self.connection.channel()

    def publish(self, message, routing_key):
        """
        Publish message to exchange using routing key

        @param message text to publish
        @param routing_key message routing key
        """
        msg = amqp.Message(message)
        msg.properties["content_type"] = "text/plain"
        msg.properties["delivery_mode"] = 2
        self.channel.basic_publish(exchange=self.exchange_name,
            routing_key=routing_key, msg=msg)

    def close(self):
        """
        Close channel and connection
        """
        self.channel.close()
        self.connection.close()
